<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');


/**
 * Parent Controller for all cms controllers.
 * @package core
 * @category Backend_Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Backend_Controller extends CI_Controller {
    
    /**
     * Constructor method.
     */
    public function __construct() {
        parent::__construct();
        
        // Mixing segment-based URL with querystrings
        parse_str($_SERVER['QUERY_STRING'], $_GET);
        
        // Disable Notice and warnings
//        error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
        
        $this->load->config('cms');
        $this->load->config('backend-authentication/ion_auth');

        $this->load->model('backend-authentication/ion_auth_model');
        $this->load->library('backend-authentication/ion_auth'); 
        
        // set cms configurations
        $cms                    = array();
        $cms['title']           = $this->config->item('title');
        $cms['db_prefix']       = $this->config->item('db_prefix');
        $this->cms = $cms;
        
        // retrieve more cms configuration from database
        $this->load->model('backend-config/cms_config_model');
        
        // set configuration from database
        $cms_configs = $this->cms_config_model->all()->get();
        foreach( $cms_configs as $cms_config )
        {
            $cms[ $cms_config['key'] ] = $cms_config['value'];
        }
        
        $this->cms = $cms;

//      $this->router->class
//        $this->router->method
        
        
//        echo '<pre>';
//        var_dump($this->ion_auth->is_admin()); 
//        echo '</pre>';
//        exit();
        
        
//        $sess = $this->session->all_userdata();
//        
//        echo '<pre>';
//        print_r($sess); 
//        echo '</pre>';
//        exit();
        
        $this->_check_login();
    }
    
    /**
     * Validates if the current user is logged in.
     * @access private
     */
    private function _check_login()
    {
        if( $this->ion_auth->logged_in() )
        {
            // load necessary files
            $this->load->model('backend-roles/role_model');
            $this->load->model('backend-modules/module_model');
            $this->load->model('backend-permissions/permission_model');
            
//            $this->load->helper('htmlpurifier');

            $this->load->library('encrypt');
            $this->encrypt->set_cipher(MCRYPT_BLOWFISH);
            $this->encrypt->set_mode(MCRYPT_MODE_CFB);
            
            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('<span class="help-block error" for="textfield">', '</span>');
            
            // retrieve user's username and ID
            $this->user_id = $this->ion_auth->user()->row()->id;
            $this->username = $this->ion_auth->user()->row()->username;

            // retrieve user's group ID
            $groups = $this->ion_auth->get_users_groups()->result_array();
            $this->group_id = $groups[0]['id'];
            
        } else
        {
            header('location:' .  base_url() . 'login');
        }
    }
    
    /**
     * Displays the 404 page not found page.
     * @access protected
     */
    protected function show_404()
    {
        // clear any views that have already been loaded
        $this->output->set_output('');
        $this->output->set_status_header('404');
        $this->template
                ->set_layout('login')
                ->build('status_codes/404');
    }
    
    protected function show_403()
    {
        $this->output->set_output('');
        $this->output->set_status_header('403');
        $this->template
                ->set_layout('login')
                ->build('status_codes/403');
    }
}

/* End of file Backend_Controller.php */
/* Location: ./system/application/core/Backend_Controller.php */